﻿#define _CRT_SECURE_NO_WARNINGS 1
#define _CRT_SECURE_NO_WARNINGS 1
#include "Snake.h"
void SetPos(short x, short y)//创建SetPos函数用来控制光标位置
{
	HANDLE houtput = NULL;
	houtput = GetStdHandle(STD_OUTPUT_HANDLE);//获得标准输出设备的句柄
	//定位光标的位置
	COORD pos = { x, y };
	SetConsoleCursorPosition(houtput, pos);//将光标位置通过句柄设置到标准输出设备中
}
void color(int c) {
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), c);
}
void WelcomeToGame()//欢迎界面的函数定义
{
	SetPos(40, 14);
	color(4);
	wprintf(L"欢迎来到贪吃蛇小游戏");
	SetPos(40, 20);//让按任意键继续出现的位置好看些
	system("pause");//暂停，按任意键继续
	system("cls");//清空屏幕
	SetPos(25, 14);
	color(6);
	wprintf(L"用←.→.↑.↓分别控制蛇的移动，F3为加速，F4为减速\n");
	SetPos(25, 15);
	color(8);
	wprintf(L"●代表蛇，★代表食物，□代表墙\n");
	SetPos(25, 16);
	color(9);
	wprintf(L"加速能够得到更高的分数\n");
	SetPos(42, 20);
	color(11);
	system("pause");
	system("cls");
}
void CreateMap()//地图函数的定义
{
	color(4);
	int i = 0;
	//最上面横一道墙
	for (i = 0; i < 29; i++)
	{
		wprintf(L"%lc", L'□');
	}
	//最下面横一道墙
	SetPos(0, 26);
	for (i = 0; i < 29; i++)
	{
		wprintf(L"%lc", L'□');
	}
	//最左面竖一道墙
	for (i = 1; i < 26; i++)
	{
		SetPos(0, i);
		wprintf(L"%lc", L'□');
	}
	//最右面竖一道墙
	for (i = 1; i < 26; i++)
	{
		SetPos(56, i);
		wprintf(L"%lc", L'□');
	}
}
void InitSnake(pSnake ps)//蛇初始化函数的定义
{
	int i = 0;
	pSnakeNode cur = NULL;
	for (int i = 0; i < 5; i++)
	{
		cur = (pSnakeNode)malloc(sizeof(SnakeNode));//创建节点空间
		if (cur == NULL)
		{
			perror("InitSnake()::malloc()");
			return;
		}
		cur->next = NULL;
		cur->y = POS_Y;
		cur->x = POS_X + 2 * i;
		//头插法插入链表
		if (ps->_pSnake == NULL)
		{
			ps->_pSnake = cur;
		}
		else//链表不为空时
		{
			cur->next = ps->_pSnake;
			ps->_pSnake = cur;
		}
		cur = ps->_pSnake;//打印蛇的身体
		while (cur)
		{
			color(13);
			SetPos(cur->x, cur->y);
			wprintf(L"%lc", BODY);
			cur = cur->next;
		}
	}
	//设置游戏属性
	ps->_foodWeight = 10;
	ps->_Socre = 0;
	ps->_SleepTime = 200;
	ps->_Dir = RIGHT;
	ps->_Status = OK;
}
void CreateFood(pSnake ps)
{
	int x = 0;
	int y = 0;
	//x的值为2的倍数，范围2~54
	//y的范围是1~25
again:
	do
	{
		x = rand() % 53 + 2;
		y = rand() % 25 + 1;
	} while (x % 2 != 0);//生成随机坐标
	pSnakeNode cur = ps->_pSnake;
	while (cur)//判断生成的食物节点坐标是否与蛇坐标重叠
	{
		if (cur->x == x || cur->y == y)
		{
			goto again;
		}
		cur = cur->next;
	}
	//创建食物的节点
	pSnakeNode pFood = (pSnakeNode)malloc(sizeof(SnakeNode));
	if (pFood == NULL)
	{
		perror("CreateFood()::malloc()");
		return;
	}
	pFood->x = x;
	pFood->y = y;
	pFood->next = NULL;
	SetPos(x, y);
	color(11);
	wprintf(L"%lc", FOOD);//打印食物
	ps->_pFood = pFood;
}
void GameStart(pSnake ps)//游戏开始初始化函数定义
{
	/*控制台窗⼝⼤⼩的设置
		• 控制台窗⼝名字的设置
		• ⿏标光标的隐藏
		• 打印欢迎界⾯
		• 创建地图
		• 初始化第蛇
		• 创建第⼀个⻝物*/
	system("mode con cols=100 lines=30");//控制台窗口的设置
	system("title 贪吃蛇");//控制台窗口名字的设置
	HANDLE houtput = GetStdHandle(STD_OUTPUT_HANDLE);//获得标准输出屏幕的句柄
	//鼠标光标的隐藏
	CONSOLE_CURSOR_INFO CursorInfo;//光标信息结构体变量创建
	GetConsoleCursorInfo(houtput, &CursorInfo);//将标准输出屏幕的光标信息存放变量中
	CursorInfo.bVisible = FALSE;//隐藏控制光标
	SetConsoleCursorInfo(houtput, &CursorInfo);//将修改的光标信息设置到标准输出屏幕中
	//打印游戏开始界面
	WelcomeToGame();
	//打印地图
	CreateMap();
	//初始化蛇
	InitSnake(ps);
	//创造一个食物
	CreateFood(ps);
}
#define KEY_PRESS(vk)  ((GetAsyncKeyState(vk)&1)?1:0)//键位虚拟值判断的宏
void PrintfHelp()//提示信息函数的定义
{
	color(6);
	SetPos(64, 14);
	wprintf(L"%ls", L"1.不能穿墙，不能咬到自己");
	SetPos(64, 15);
	wprintf(L"%ls", L"2.用 ↑. ↓ . ← . → 来控制蛇的移动");
	SetPos(64, 16);
	wprintf(L"%ls", L"3.按F3加速，F4减速");
	SetPos(64, 17);
	wprintf(L"%ls", L"4.按ESC退出游戏，按空格暂停游戏");
	SetPos(64, 18);
	wprintf(L"%ls", L"5.小张同学制作");

}
void Pause()//暂停函数的定义
{
	while (1)
	{
		Sleep(200);
		if (KEY_PRESS(VK_SPACE))
		{
			break;
		}
	}
}
int NextFood(pSnakeNode pn, pSnake ps)//检测下一个节点坐标的函数
{
	return (pn->x == ps->_pFood->x && pn->y == ps->_pFood->y);
}
void EatFood(pSnakeNode pn, pSnake ps)//吃掉食物的函数
{
	//将食物节点作为新节点头插到蛇头
	ps->_pFood->next = ps->_pSnake;
	ps->_pSnake = ps->_pFood;
	//将下一个节点释放
	free(pn);
	pn = NULL;
	pSnakeNode cur = ps->_pSnake;
	//打印吃掉后的蛇
	while (cur)
	{
		SetPos(cur->x, cur->y);
		color(13);
		wprintf(L"%lc", BODY);
		cur = cur->next;
	}
	//分数增加
	ps->_Socre += ps->_foodWeight;
	//重新创建食物
	CreateFood(ps);
}
void NoFood(pSnakeNode pn, pSnake ps)
{
	//将下一个坐标头插到蛇
	pn->next = ps->_pSnake;
	ps->_pSnake = pn;
	//打印移动后的蛇
	pSnakeNode cur = ps->_pSnake;
	while (cur->next->next != NULL)
	{
		SetPos(cur->x, cur->y);
		color(13);
		wprintf(L"%lc", BODY);
		cur = cur->next;
	}
	//把最后一个节点打印成空格
	SetPos(cur->next->x, cur->next->y);
	printf("  ");
	//释放最后一个节点
	free(cur->next);
	//把倒数第二个节点next置为空
	cur->next = NULL;
}
//检测是否撞到墙
void KillByWall(pSnake ps)
{
	if (ps->_pSnake->x == 0 || ps->_pSnake->y == 0 || ps->_pSnake->y == 26 || ps->_pSnake->x == 56)
	{
		ps->_Status = KILL_BY_WALL;
	}
}
//检测是否撞到自己
void KillBySelf(pSnake ps)
{
	pSnakeNode cur = ps->_pSnake->next;
	while (cur)
	{
		if (cur->x == ps->_pSnake->x && cur->y == ps->_pSnake->y)
		{
			ps->_Status = KILL_BY_SELE;
			break;
		}
		cur = cur->next;
	}
}
void SnakeMove(pSnake ps)//蛇每走一步的函数的定义
{
	pSnakeNode pNextNode = (pSnakeNode)malloc(sizeof(SnakeNode));//创建走的下一个节点
	if (pNextNode == NULL)
	{
		perror("SnakeMove()::malloc()");
		return;
	}
	switch (ps->_Dir)//方向判断
	{
	case UP:
		pNextNode->x = ps->_pSnake->x;
		pNextNode->y = ps->_pSnake->y - 1;
		break;
	case DOWN:
		pNextNode->x = ps->_pSnake->x;
		pNextNode->y = ps->_pSnake->y + 1;
		break;
	case LEFT:
		pNextNode->x = ps->_pSnake->x - 2;
		pNextNode->y = ps->_pSnake->y;
		break;
	case RIGHT:
		pNextNode->x = ps->_pSnake->x + 2;
		pNextNode->y = ps->_pSnake->y;
		break;
	}
	//检测下一个坐标是否是食物
	if (NextFood(pNextNode, ps))
	{
		EatFood(pNextNode, ps);
	}
	else
	{
		NoFood(pNextNode, ps);
	}
	//检测是否撞到墙
	KillByWall(ps);
	//检测是否撞到自己
	KillBySelf(ps);
}

//运行游戏主逻辑函数的定义
void GameRun(pSnake ps)
{
	PrintfHelp();//打印帮助信息
	color(9);
	do
	{
		//打印总分数与食物分数的信息
		SetPos(64, 10);
		printf("总分数:%d\n", ps->_Socre);
		SetPos(64, 11);
		color(9);
		printf("当前食物的分数：%2d\n", ps->_foodWeight);
		//键位的操作
		if (KEY_PRESS(VK_UP) && ps->_Dir != DOWN)//↑键，是蛇头方向向上
		{
			ps->_Dir = UP;
		}
		else if (KEY_PRESS(VK_DOWN) && ps->_Dir != UP)//↓键，是蛇头方向向下
		{
			ps->_Dir = DOWN;
		}
		else if (KEY_PRESS(VK_LEFT) && ps->_Dir != RIGHT)//→键，是蛇头方向向右
		{
			ps->_Dir = LEFT;
		}
		else if (KEY_PRESS(VK_RIGHT) && ps->_Dir != LEFT)//←键，是蛇头方向向左
		{
			ps->_Dir = RIGHT;
		}
		else if (KEY_PRESS(VK_SPACE))//空格键，表示暂停
		{
			Pause();//暂停函数
		}
		else if (KEY_PRESS(VK_F3))//F3加速
		{
			if (ps->_SleepTime > 60)
			{
				ps->_SleepTime -= 30;
				ps->_foodWeight += 2;
			}
		}
		else if (KEY_PRESS(VK_F4))//F4减速
		{
			if (ps->_foodWeight > 2)
			{
				ps->_SleepTime += 30;
				ps->_foodWeight -= 2;
			}
		}
		else if (KEY_PRESS(VK_ESCAPE))//Esc键表示游戏结束
		{
			ps->_Status = END_NOMAL;
		}
		SnakeMove(ps);//蛇每走一步的函数
		Sleep(ps->_SleepTime);//睡眠时间

	} while (ps->_Status == OK);

}
void GameEnd(pSnake ps)//游戏结束主逻辑函数
{
	SetPos(24, 12);
	switch (ps->_Status)
	{
	case END_NOMAL:
		color(8);
		wprintf(L"你主动退出游戏\n");
		break;
	case KILL_BY_WALL:
		color(8);
		wprintf(L"你撞到墙上，游戏结束\n");
		break;
	case KILL_BY_SELE:
		color(8);
		wprintf(L"你撞到了自己，游戏结束\n");
		break;
	}
	//释放蛇身链表
	pSnakeNode cur = ps->_pSnake;
	while (cur)
	{
		pSnakeNode del = cur;
		cur = cur->next;
		free(del);
	}

}